package com.zhiyou100.crowd.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.org.apache.regexp.internal.recompile;
import com.zhiyou100.IService.IuserServiceaaaaa;
import com.zhiyou100.entity.TbFunding;
import com.zhiyou100.entity.TbLike;
import com.zhiyou100.entity.Tbuspts;
import com.zhiyou100.entity.Useraudit;
import com.zhiyou100.entity.prosGo.TbProjects;
import com.zhiyou100.entity.prosGo.TbUser;
import com.zhiyou100.pojo.TbComments;


@Controller
@RequestMapping("user")
public class UserController {
	@Resource(name="userService")
	private IuserServiceaaaaa userService;
	/**
	 * 注册方法
	 * @param tbUser
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addUser.do")
	public String addUser(TbUser tbUser) {
		boolean flag = userService.addUser(tbUser);
		if(flag == true) 
			return "2";
		else
			return "1";
	}
	
 
	/**
	 * 
	 * 登录方法
	 * @param request
	 * @param tbUser
	 * @return
	 */
	@ResponseBody
	@RequestMapping("longinTbUser.do")
    public String login(HttpServletRequest request , TbUser tbUser) {
		TbUser tbUser2 = userService.login(tbUser);
		if(tbUser2!=null) {
			request.getSession().setAttribute("tbUser2", tbUser2);
			return "1";
		}else {
			return "2";
		}
	}
	/**
	 * 查询发起的项目
	 */
	@RequestMapping("query.do")
	public String query(@RequestParam(required=true,defaultValue="1") Integer page,
			HttpServletRequest request ,Tbuspts tbuspts ,String paths) {
		//获取域中对象
		TbUser tbUser = (TbUser) request.getSession().getAttribute("tbUser2");
		tbuspts.setUsAccount(tbUser.getUsAccount());
		PageHelper.startPage(page, 3);
		List<Tbuspts> tbuspts_list = userService.queryuser(tbuspts);
		PageInfo<Tbuspts> p = new PageInfo<Tbuspts>(tbuspts_list);
		request.setAttribute("page", p);
		request.setAttribute("tbuspts_list", tbuspts_list);
		return paths;
	}
	
	@RequestMapping("queryUser.do")
	public String query(HttpServletRequest request ,TbUser tbUser,String paths) {
		//获取域中对象
		TbUser tbUser1 = (TbUser) request.getSession().getAttribute("tbUser2");
		tbUser.setUsAccount(tbUser1.getUsAccount());
		List<TbUser> tbUsers = userService.queryAcouut(tbUser);
		request.setAttribute("tbUsers", tbUsers.get(0));
		
		return paths;
	}	
	
	/**
	 * 
	 * 修改方法
	 * @param tbUser
	 * @return
	 */
	@RequestMapping("update.do")
	public String queryUsId(TbUser tbUser){
		 int row = userService.update(tbUser);
		return "forward:queryUser.do?paths=userzl";
	}
	
	@ResponseBody
	@RequestMapping("addUseraudit.do")
	public String addUseraudit(HttpServletRequest request ,Useraudit useraudit) {
		TbUser tbUser1 = (TbUser) request.getSession().getAttribute("tbUser2");
		useraudit.setaUId(tbUser1.getUsId());
		Useraudit useraudit2 = (Useraudit) request.getSession().getAttribute("useraudit1");
		useraudit.setaFimg(useraudit2.getaFimg());
		boolean flag = userService.addUseraudit(useraudit);
		if(flag == true) 
			return "2";
		else
			return "1";
	}
	@RequestMapping("queryLike")
	public String queryLike(HttpServletRequest request ,TbLike tblike) {
		TbUser tbUser1 = (TbUser) request.getSession().getAttribute("tbUser2");
		tbUser1.getUsId();
		tblike.setuId(tbUser1.getUsId());
		return "";
	}
	/**
	 * 退出方法
	 * @param request
	 * @return
	 */
	@RequestMapping("quit.do")
	public String quit(HttpServletRequest request) {
//		TbUser tbUser1 = (TbUser) request.getSession().getAttribute("tbUser2");
		request.getSession().removeAttribute("tbUser2");
		request.getSession().removeAttribute("useraudit1");
		return "index";
	}
	/**
	 * 文件上传
	 * @author AnTree
	 *
	 */
		@RequestMapping("upload.do")
		public String upload(@RequestParam("file") MultipartFile file,
				HttpServletRequest request ,Useraudit useraudit
				) {
			TbUser tbUser1 = (TbUser) request.getSession().getAttribute("tbUser2");
			useraudit.setaUId(tbUser1.getUsId());
			//判断文件是否为null
			if(!file.isEmpty()) {
				//设置文件的保存路径，获取项目路径
				//加入我们要上传图片，图片一般保存在img路径下
				String path = request.getSession().getServletContext().getRealPath("/img/");
				System.out.println("上传路径==》"+path);
				//文件名,可以通过MultipartFile对象获取文件名
				String filename = file.getOriginalFilename();
				//创建File对象，最后我们将创建的File对象上传走
				File upload_file = new File(path, filename);
				System.out.println("文件名==》"+filename);
				//判断路径是否存在
				if(!upload_file.getParentFile().exists()) {
					//创建路径
					upload_file.getParentFile().mkdirs();
				}
				//将文件上传，使用MultipartFile对象中的transferTo将文件上传
				try {
					file.transferTo(upload_file);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				//返回成功页面
				try {
					useraudit.setaFimg("img\\"+new String(filename.getBytes(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				request.getSession().setAttribute("useraudit1", useraudit);
				return "userrz";
				}else {
					request.setAttribute("message", "上传失败");
					//上传失败
					return "userrz ";
				}
			}
	

		/**
		 * 
		 * 查询支持项目
		 * 
		 */
		@RequestMapping("funding.do")
		public String funding(@RequestParam(required=true,defaultValue="1") Integer page,
				HttpServletRequest request , TbFunding tbfunding) {
			//获取域中对象
			TbUser tbUser = (TbUser) request.getSession().getAttribute("tbUser2");
			tbfunding.setFdUsId(tbUser.getUsId());
			System.out.println(tbfunding);
			PageHelper.startPage(page, 3);
			List<TbFunding> TbFundings = userService.queryfund(tbfunding);
			PageInfo<TbFunding> p = new PageInfo<TbFunding>(TbFundings);
			request.setAttribute("page", p);
			request.setAttribute("TbFundings", TbFundings);
			System.out.println(TbFundings);
			return "usercy";
		}
		/**
		 * 
		 * 查询点赞项目
		 * 
		 */
		@RequestMapping("like.do")
		public String funding(@RequestParam(required=true,defaultValue="1") Integer page,
				HttpServletRequest request , TbLike tbLike) {
			//获取域中对象
			TbUser tbUser = (TbUser) request.getSession().getAttribute("tbUser2");
			tbLike.setuId(tbUser.getUsId());
			PageHelper.startPage(page, 3);
			List<TbLike> TbLike = userService.querylike(tbLike);
			PageInfo<TbLike> p = new PageInfo<TbLike>(TbLike);
			request.setAttribute("page", p);
			request.setAttribute("TbLikes", TbLike);
			return "userdz";
		}
		/**
		 * 
		 * 查询资金
		 * 
		 * 
		 */
		@RequestMapping("money.do")
		public String funding(@RequestParam(required=true,defaultValue="1") Integer page,
				HttpServletRequest request , TbProjects tbProjects) {
			//获取域中对象
			TbUser tbUser = (TbUser) request.getSession().getAttribute("tbUser2");
			tbProjects.setPsUsId(tbUser.getUsId());
			PageHelper.startPage(page, 3);
			List<TbProjects> tbProjects1 = userService.querytbProjects(tbProjects);
			PageInfo<TbProjects> p = new PageInfo<TbProjects>(tbProjects1);
			request.setAttribute("page", p);
			request.setAttribute("tbProjects1", tbProjects1);
			return "userzj";
		}
		/**
		 * 查询评论
		 * 
		 */
		@RequestMapping("comment.do")
		public String funding(@RequestParam(required=true,defaultValue="1") Integer page,
				HttpServletRequest request , TbComments tbComments) {
			//获取域中对象
			TbUser tbUser = (TbUser) request.getSession().getAttribute("tbUser2");
			tbComments.setCmUsId(tbUser.getUsId());
			PageHelper.startPage(page, 3);
			List<TbComments> tbComments1 = userService.querytbComment(tbComments);
			PageInfo<TbComments> p = new PageInfo<TbComments>(tbComments1);
			request.setAttribute("page", p);
			request.setAttribute("tbComments1", tbComments1);
			return "userpl";
		}
		
		

}